<?php
    session_start();
	$_title='个人主页|我的账号';
	include_once 'head.php';
	//左侧导航
	$my_label='account';
	$smarty->assign('my_label',$my_label);		
	$userc=new user();
	// $user_id=2;

	$apic=new api();

	//发送短信
	if($_GET['action']=='mobile'){
		//查看是否已注册
		$re = $commonc->getInfo("user WHERE phone='".$_POST['mobile']."'");
		if($re==false){
			//1.设置信息
			$mobile=$_POST['mobile'];
			$code = rand(100000,999999);
			$data ="您好！验证码是：" . $code ." 请尽快使用";
			$_SESSION['code'] = $code;
			//2.发送短信
			$result=$apic->send($mobile,$data);
			$result=explode(',',$result);
			$result=$result['1'];

			$error=0;	//没有错误
		}else{
			$error=1;	//该手机已存在
		}

		echo json_encode(array('error'=>$error,'result'=>$result));
		exit;
	}


	//读取作者个人信息
	if($user_id!='' && $user_id!=null){
		$key='i.*';
		$user=$userc->userInfo($user_id,$key);
		$smarty->assign('user',$user);
	}else{
		header('Location:login.php');
	}	

	$smarty->assign('user',$user);
	// var_dump($user);

	if(@$_POST['submit']){
		$phone=$_POST['phone'];
		$email=$_POST['email'];

		$code = $_POST['code']; //收到的验证码
	
	
		if ($code==$_SESSION['code'] || $phone==''){
			$avatar=$user['avatar'];
			//上传图片，生成缩略图
			if($_FILES['avatar']['name']!=''){
				//设置路径
				$folder = "./upload/user/";
				$annexFolder = $folder."large/";
				$smallFolder = $folder."small/";
				//删除旧图
				@unlink($annexFolder.$avatar);
				@unlink($smallFolder.$avatar);
				//上传新图
				$upimg = new uploadImg();
				$avatar = $upimg->upLoad('avatar',$annexFolder);
				//小图
				$upimg->smallImg($annexFolder.$avatar,$smallFolder);
			}

			$arr=array('account'=>$_POST['account'],
					   'avatar'=>$avatar,);
			if($phone!='' && $phone!=null){$arr['phone']=$phone;}
			if($email!='' && $email!=null){$arr['email']=$email;}

			$res=$userc->userInfoUpdate($arr,'id="'.$user_id.'"');
			if($res==1){
				$msg='信息保存成功！';
			}else{
				$msg="信息保存失败！";
			}
			$url='my_account.php';
			echo $commonc->submitAlert($msg,$url);
			exit();
			
		}else{
			$msg="验证码错误！";
			$url='my_account.php';
			echo $commonc->submitAlert($msg,$url);
		}
	}

	//html模板区分
	$html='my_account.html';
	$smarty->display($html);		
?>